#!/bin/bash

# 记录带时间戳的日志
log() {
    local level=$1
    shift
    printf "$(date +'%Y/%m/%d %H:%M:%S.000')|%-4s| %s\n" "$level" "$@"
}

WORKSPACE=$(dirname "$0")
folderName=$(basename -- "$WORKSPACE")
log INFO "=========================================================================================="
log INFO "Starting configuration generation for: ${folderName}"

# 路径配置
LUBAN_DLL="${WORKSPACE}/../../Tools/Luban/Luban.dll"
CUSTOM_TEMPLATES="${WORKSPACE}/../../Tools/CustomTemplates"
PYTHON_GEN_DIR="${WORKSPACE}/../../Tools/AnalyticsPython"
PROTO3_TOOL="${WORKSPACE}/../../Tools/protobuf-31.1-tools/protoc-31.1-osx-aarch_64/bin/protoc"

# 输出目录
GENERATE_BIN_CODE="${WORKSPACE}/Generated/client-code-cs-bin"
GENERATE_JSON_CODE="${WORKSPACE}/Generated/client-code-cs-json"
CS_GENERATE_PROTOBUF3_CODE="${WORKSPACE}/Generated/client-code-cs-protobuf3"
GENERATE_JSON_DATA="${WORKSPACE}/Generated/client-data-json"
GENERATE_BIN_DATA="${WORKSPACE}/Generated/client-data-bin"
GENERATE_PROTOBUF3_PROTO_FILE="${WORKSPACE}/Generated/client-data-protobuf3-proto"
GENERATE_PROTOBUF3_DATA="${WORKSPACE}/Generated/client-data-protobuf3-bin"

# 参数处理（带默认值）
DOTNET_PATH="${1:-dotnet}"
RUN_LUBAN="${2:-true}"
RUN_PYTHON="${3:-true}"

# 显示执行配置
log INFO "Execution Config:"
log INFO "-- Dotnet Path: ${DOTNET_PATH}"
log INFO "-- Run Luban: ${RUN_LUBAN}"
log INFO "-- Run Analytics: ${RUN_PYTHON}"

# Python 分析代码生成
if [[ "${RUN_PYTHON}" == "true" ]]; then
    log INFO "========================================"
    log INFO "Generating analytics tables [打点表生成]"
    log INFO "========================================"
    
    if ! python3 "${PYTHON_GEN_DIR}/AnalyticsCodeCreator.py" "${folderName}"; then
        log ERROR "Analytics generation failed with exit code $?"
        exit 1
    fi
    log SUCCESS "Analytics generated successfully"
fi

# 终止条件检查
if [[ "${RUN_LUBAN}" != "true" ]]; then 
    log INFO "Skipping Luban execution as requested"
    exit 0
fi

# Luban 配置生成
log INFO "========================================"
log INFO "Starting Luban data generation [LuBan]"
log INFO "========================================"
log INFO "-- Luban DLL:   ${LUBAN_DLL}"
log INFO "-- Config File: ${WORKSPACE}/luban.conf"

# 检查必要文件是否存在
[[ -f "${LUBAN_DLL}" ]] || { log ERROR "Luban.dll not found at ${LUBAN_DLL}"; exit 1; }
[[ -f "${WORKSPACE}/luban.conf" ]] || { log ERROR "luban.conf not found"; exit 1; }

# 创建输出目录
mkdir -p "${GENERATE_JSON_CODE}" \
          "${GENERATE_JSON_DATA}" \
          "${GENERATE_BIN_CODE}" \
          "${GENERATE_BIN_DATA}" 
          
#          "${CS_GENERATE_PROTOBUF3_CODE}" \
#          "${GENERATE_PROTOBUF3_DATA}" \
#          "${GENERATE_PROTOBUF3_PROTO_FILE}" \

# 执行 Luban 生成命令
if ! "${DOTNET_PATH}" "${LUBAN_DLL}" \
    --conf "${WORKSPACE}/luban.conf" \
    --customTemplateDir "${CUSTOM_TEMPLATES}" \
    --validationFailAsError true \
    -t client \
    -c cs-simple-json \
    -d json \
    -x "json.outputDataDir=${GENERATE_JSON_DATA}" \
    -x "cs-simple-json.outputCodeDir=${GENERATE_JSON_CODE}" \
    -c cs-bin \
    -d bin \
    -x "cs-bin.outputCodeDir=${GENERATE_BIN_CODE}" \
    -x "bin.outputDataDir=${GENERATE_BIN_DATA}" 
    
#    -x codeStyle=none \
#    -x "cs-protobuf3.outputCodeDir=${CS_GENERATE_PROTOBUF3_CODE}" \
#    -x "protobuf3-bin.outputDataDir=${GENERATE_PROTOBUF3_DATA}" \
#    -x "protobuf3.outputCodeDir=${GENERATE_PROTOBUF3_PROTO_FILE}" \
#    -c cs-protobuf3 \
#    -c protobuf3 \
#    -d protobuf3-bin \

then
    log ERROR "Luban generation failed !!!"
    exit 1
fi

#log SUCCESS "Generate Proto To C# File..."
#
#chmod +x $PROTO3_TOOL
#$PROTO3_TOOL -I="${GENERATE_PROTOBUF3_PROTO_FILE}" --csharp_out="${CS_GENERATE_PROTOBUF3_CODE}" "schema.proto"
#
#PROTO3_EXIT=$?
#
#if [[ $PROTO3_EXIT -ne 0 ]]; then
#    log ERROR "=========================================================================================="
#    log ERROR "Proto3 export failed! Please check the error!!! exit code: $PROTO3_EXIT"
#    log ERROR "=========================================================================================="
#    exit $PROTO3_EXIT
#fi
#
#mv "${CS_GENERATE_PROTOBUF3_CODE}/schema.cs" "${CS_GENERATE_PROTOBUF3_CODE}/GeneratedConfigDefines.cs"
#MV_EXIT=$?
#
#if [[ $MV_EXIT -ne 0 ]]; then
#    log ERROR "=========================================================================================="
#    log ERROR "Proto3 generated cs file rename failed! Please check the error!!! exit code: $MV_EXIT"
#    log ERROR "=========================================================================================="
#    exit $MV_EXIT
#fi

log SUCCESS "========================================"
log SUCCESS "Configuration generation completed!"
log SUCCESS "Output Locations:"
log SUCCESS "-- JSON Code:   ${GENERATE_JSON_CODE}"
log SUCCESS "-- JSON Data:   ${GENERATE_JSON_DATA}"
log SUCCESS "-- Binary Code: ${GENERATE_BIN_CODE}"
log SUCCESS "-- Binary Data: ${GENERATE_BIN_DATA}"
#log SUCCESS "-- PROTO3 Code:   ${CS_GENERATE_PROTOBUF3_CODE}"
#log SUCCESS "-- PROTO3 Data:   ${GENERATE_PROTOBUF3_DATA}"